#include <QList>
#include "gui/itemviewadddialog.h"
#include "gui/separatorwidget.h"
#include "core/suppliercontroller.h"
#include "core/suppliermodel.h"
#include "core/constants.h"

void ItemViewAddDialog::onSaveButtonClicked()
{
    if (nameEdit->text() == "" || codeEdit->text() == ""
            || srpEdit->text() == "") {
        QMessageBox::critical(this,
                              "Incomplete Item Data",
                              "You must fill in all fields before you can "
                              "save this Item.");
        return;
    }

    if (!this->item) {
        this->item = new ItemModel;
    }

    SupplierController *supplierCtrl = new SupplierController;

    item->setName(this->nameEdit->text());
    item->setCode(this->codeEdit->text());
    item->setSrp(this->srpEdit->text().toFloat());

    item->setSupplier(supplierCtrl->getSupplierByName(this->supplierBox->currentText()));

    this->accept();
}

QComboBox *ItemViewAddDialog::createSupplierBox()
{
    SupplierController *ctrl = new SupplierController;
    QList<BaseModel *> models = ctrl->getAll();
    QComboBox *comboBox = new QComboBox;
    SupplierModel *supplier;
    int i;

    for (i = 0; i < models.length(); i++) {
        supplier = (SupplierModel *) models.at(i);
        comboBox->addItem(supplier->getName(), supplier->getID());
    }

    delete ctrl;
    return comboBox;
}

ItemModel *ItemViewAddDialog::getItem() {
    return this->item;
}

void ItemViewAddDialog::initUiComponents()
{
    QFont font;
    mainLayout = new QVBoxLayout;
    gridLayout = new QGridLayout;

    headerLabel = new QLabel("Add an Item");
    font = headerLabel->font();
    font.setPointSize(font.pointSize() + HEADER_UPSIZE);
    headerLabel->setFont(font);

    subHeaderLabel = new QLabel("Enter your new Item's details.");

    nameLabel = new QLabel;
    nameEdit = new QLineEdit;

    codeLabel = new QLabel;
    codeEdit = new QLineEdit;

    supplierLabel = new QLabel;
    supplierBox = this->createSupplierBox();

    srpLabel = new QLabel;
    srpEdit = new QLineEdit;

    mainLayout->addWidget(headerLabel);
    mainLayout->addWidget(subHeaderLabel);
    mainLayout->addWidget(new SeparatorWidget);

    nameLabel->setText("Product Name");
    gridLayout->addWidget(nameLabel, 0, 0);
    gridLayout->addWidget(nameEdit, 0, 1);

    codeLabel->setText("Product Code");
    gridLayout->addWidget(codeLabel, 1, 0);
    gridLayout->addWidget(codeEdit, 1, 1);

    supplierLabel->setText("Supplier Name");
    gridLayout->addWidget(supplierLabel, 2, 0);

    gridLayout->addWidget(supplierBox, 2, 1);

    srpLabel->setText("SRP");
    gridLayout->addWidget(srpLabel, 3, 0);
    gridLayout->addWidget(srpEdit, 3, 1);

    mainLayout->addLayout(gridLayout);
    mainLayout->addStretch();

    mainLayout->addWidget(new SeparatorWidget);

    buttonBox = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel);
    mainLayout->addWidget(buttonBox);
    this->setLayout(mainLayout);
}

void ItemViewAddDialog::initSignalsAndSlots()
{
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(onSaveButtonClicked()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}

ItemViewAddDialog::ItemViewAddDialog(QWidget *parent) :
    QDialog(parent)
{
    this->setWindowTitle("Add Item");
    this->setMinimumSize(500, 320);
    this->initUiComponents();
    this->initSignalsAndSlots();
    this->item = NULL;
}
